package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.SysUserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.net.http.HttpRequest;

@Controller
public class LoginController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public String toLogin()
    {
        return "forward:/login.jsp";
    }

    @PostMapping("/login")
    public String login(@RequestParam("account") String account, @RequestParam("password") String password,
                        HttpServletRequest request, HttpSession  session){
        SysUser loginUSer = sysUserService.login(account, password);
        if (loginUSer != null){
            //登录成功
            session.setAttribute("loginUSer", loginUSer);
            //重定向返回首页
            return "redirect:/main/index";
        }else {
            //登录失败
            request.setAttribute("error", "用户名或密码错误");
            return "forward:/login.jsp";
        }
    }

    //跳转到后台首页
    @GetMapping("/main/index")
    public String index(){
        return "index";
    }

}
